package com.maxafaa.motionsensordemo;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainSurfaceView extends SurfaceView  implements SurfaceHolder.Callback, Runnable, SensorEventListener{

	private boolean m_is_running;
	private SurfaceHolder m_surfaceholder;
	private static int s_screen_width, s_screen_height;
	private Paint m_paint;
	private Thread m_mainthread;
	private Canvas m_lockcanvas;
	private float m_touch_x, m_touch_y;
	private float m_ball_heart_x, m_ball_heart_y;
	private final int M_BALL_RADIUS = 20;
	//Motion Sensor
	private Context m_context;
	private SensorManager m_sensor_manager;
	private Sensor m_sensor;
	private float m_sensor_x_value, m_sensor_y_value, m_sensor_z_value;
	
	
	public MainSurfaceView(Context context) {
		super(context);
		m_context = context;
		m_surfaceholder = getHolder();
		m_surfaceholder.addCallback(this);		
		setFocusable(true);
		s_screen_width = context.getResources().getDisplayMetrics().widthPixels;
		s_screen_height = context.getResources().getDisplayMetrics().heightPixels;
	}

	private void Initialize(){
		
		m_sensor_manager = (SensorManager) m_context.getSystemService(Context.SENSOR_SERVICE);
		m_sensor = m_sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		m_sensor_manager.registerListener(this, m_sensor, SensorManager.SENSOR_DELAY_GAME);
		m_ball_heart_x = s_screen_width/2;
		m_ball_heart_y = s_screen_height/2;
		m_sensor_x_value = 0;
		m_sensor_y_value = 0;
		m_sensor_z_value = 0;
		m_touch_x = -1;
		m_touch_y = -1;
		m_paint = new Paint();
		m_mainthread = new Thread(this);
		m_mainthread.start();
		m_is_running = true;
		
		
	}
	
	
	public void GameCreate(){

	}
	
	public void GameStart(){

	}
	
	public void GamePause(){
		if(m_sensor_manager != null)
			m_sensor_manager.unregisterListener(this);
	}
	
	public void GameResume(){
		if(m_sensor_manager != null)
			m_sensor_manager.registerListener(this, m_sensor, SensorManager.SENSOR_DELAY_GAME);

	}
	
	public void GameStop(){

	}
	
	public void GameDestroy(){

	}
	
	public void GameRestart(){

	}
	
	private void GameUpdate(){
		
	}
	
	private void GameDraw(Canvas canvas){
		canvas.drawColor(Color.WHITE);
		m_paint.setColor(Color.BLACK);
		canvas.drawText("X Value: " + m_sensor_x_value, 5, 15, m_paint);
		canvas.drawText("Y Value: " + m_sensor_y_value, 5, 30, m_paint);
		canvas.drawText("Z Value: " + m_sensor_z_value, 5, 45, m_paint);
		m_paint.setColor(Color.BLUE);
		m_paint.setAntiAlias(true);		
		canvas.drawCircle(m_ball_heart_x, m_ball_heart_y, M_BALL_RADIUS, m_paint);
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		super.onTouchEvent(e);
		m_touch_x = e.getX();
		m_touch_y = e.getY();
		return true;
	}
	

	@Override
	public void run() {
		while(m_is_running){
			try{
				// Thread.sleep(20);
			}catch(Exception e){}
			GameUpdate();
			m_lockcanvas = m_surfaceholder.lockCanvas(null);
			synchronized (m_surfaceholder) {
				if(m_lockcanvas != null){
					GameDraw(m_lockcanvas);
					m_surfaceholder.unlockCanvasAndPost(m_lockcanvas);
				}
			}
		}		
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Initialize();		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		m_is_running = false;
        do {
            try {
                m_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);		
	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		m_sensor_x_value = event.values[0];//x > 0 => tilt to left
		m_sensor_y_value = event.values[1];//y > 0 => tilt to bottom
		m_sensor_z_value = event.values[2];//z > 0 => screen is up
		m_ball_heart_x -= m_sensor_x_value;
		if(m_ball_heart_x - M_BALL_RADIUS < 0)
			m_ball_heart_x = M_BALL_RADIUS;
		if(m_ball_heart_x + M_BALL_RADIUS > s_screen_width)
			m_ball_heart_x = s_screen_width - M_BALL_RADIUS;
		m_ball_heart_y += m_sensor_y_value;
		if(m_ball_heart_y - M_BALL_RADIUS < 0)
			m_ball_heart_y = M_BALL_RADIUS;
		if(m_ball_heart_y + M_BALL_RADIUS > s_screen_height)
			m_ball_heart_y = s_screen_height - M_BALL_RADIUS;
		
	}
}
